<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('采集任务信息列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>任务名称：</label>
                                <input type="text" name="taskName"/>
                            </li>
                            <li>
                                <label>目标模型：</label>
                                <input type="text" name="targetTable"/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="system:task:add">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="system:task:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="system:task:remove">
                    <i class="fa fa-remove"></i> 删除
                </a>
<!--                <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="system:task:export">-->
<!--                    <i class="fa fa-download"></i> 导出-->
<!--                </a>-->
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('system:task:edit')}]];
        var removeFlag = [[${@permission.hasPermi('system:task:remove')}]];
        var prefix = ctx + "system/task";

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: "采集任务信息",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'id',
                    title: '任务唯一标识',
                    visible: false
                },
                // {
                //     field: 'projectName',
                //     title: '所属项目名称'
                // },
                {
                    field: 'taskName',
                    title: '任务名称'
                },
                {
                    field: 'taskType',
                    title: '任务类型'
                },
                {
                    field: 'cronExpression',
                    title: '调度表达式'
                },
                {
                    field: 'triggerTime',
                    title: '触发时间'
                },
                {
                    field: 'taskStatus',
                    title: '任务状态'
                },
                {
                    field: 'createTime',
                    title: '任务创建时间'
                },
                {
                    field: 'dataSource',
                    title: '数据源'
                },
                {
                    field: 'targetTable',
                    title: '目标模型'
                },
                {
                    field: 'executionSql',
                    title: '执行 SQL 语句内容'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];

                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
                        actions.push('<a class="btn btn-info btn-xs execute-btn" href="javascript:void(0)" onclick="executeTaskWithLoading(\'' + row.id + '\', this)"><i class="fa fa-play"></i>执行</a>');
                        // actions.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="executeTask(\'' + row.id + '\')"><i class="fa fa-play"></i>执行</a>');
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });
        function executeTaskWithLoading(id, element) {
            console.log(ctx, '这是啥')
            // 设置按钮为禁用并添加 loading 动画
            $(element).prop("disabled", true).find("i").addClass("fa-spin");

            // 延迟 3 秒后执行请求
            setTimeout(function () {
                $.ajax({
                    url: prefix + "/execute",
                    type: "POST",
                    data: { "id": id },
                    success: function (result) {
                        if (result.code == 0) {
                            $.modal.alertSuccess("执行成功");
                            $.table.refresh();
                        } else {
                            $.modal.alertError(result.msg);
                        }
                    },
                    complete: function () {
                        // 恢复按钮状态
                        $(element).prop("disabled", false).find("i").removeClass("fa-spin");
                    }
                });
            }, 3000); // 3秒延迟
        }


        function executeTask(id) {
            $.ajax({
                url: prefix + "/execute",
                type: "POST",
                data: { "id": id },
                success: function(result) {
                    if (result.code == 0) {
                        $.modal.alertSuccess("执行成功");
                        $.table.refresh();
                    } else {
                        $.modal.alertError(result.msg);
                    }
                }
            });
        }
    </script>
</body>
</html>
